import 'package:package_info_plus/package_info_plus.dart';

class CXAppUtils {
  static PackageInfo? _packageInfo;

  /// 异步获取 PackageInfo 对象，用于内部使用
  static Future<PackageInfo?> _getPackageInfo() async {
    _packageInfo ??= await PackageInfo.fromPlatform();
    return _packageInfo;
  }

  /// 获取 app 名字
  static Future<String?> getAppName() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.appName;
  }

  /// 获取包名
  static Future<String?> getPackageName() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.packageName;
  }

  /// 版本名
  static Future<String?> getVersionName() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.version;
  }

  /// 版本号
  static Future<String?> getVersionNumber() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.buildNumber;
  }

  /// 获取App构建签名
  /// 在 iOS 上为空字符串，在 Android 上为密钥签名(十六进制)
  static Future<String?> getBuildSignature() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.buildSignature;
  }

  /// 获取App安装商店
  /// 若是通过应用商店安装的应用，返回应用商店的名称，否则返回空字符串
  static Future<String?> getInstallerStore() async {
    final packageInfo = await _getPackageInfo();
    return packageInfo?.installerStore;
  }
}
